package net.trustie.utils;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.List;

/**
 * Created by LouAnt on 2017/2/10 0010
 * Description：抽取openhub中多链接的项目
 */
public class ExtractMutilLink4Openhub implements PageProcessor{
    private Site site = Site.me().setRetryTimes(5).setSleepTime(500);

    private static List<String> homepages;
    private static Page _page;

    static ExtractMutilLink4Openhub extractMutilLink4Openhub = new ExtractMutilLink4Openhub();

    @Override
    public void process(Page page) {
        //抽取Homepage列表，Html格式参考 https://www.openhub.net/p/zimbra/links
        ExtractMutilLink4Openhub.homepages = page.getHtml().xpath("//div[@class='links center-block']/div[1]/div[2]/div/h5/a").links().all();
    }

    @Override
    public Site getSite() {
        return site;
    }

    public List<String> getHomepages(String url){
        try{
            int count = 10;
            //为防止一次抽取不成功，抽取10次
            while(ExtractMutilLink4Openhub.homepages==null && count > 0 ) {
                ExtractMutilLink4Openhub.doExtract(url);
                count--;
            }
        }catch(Exception e) {


        }
        return ExtractMutilLink4Openhub.homepages;
    }

    /**
     * 获取多个link，用“;”间隔.此方法为二次抽取的入口方法
     * @param url
     * @return
     */
    public  String extractLinks(String url){
        String homepage = "";
        doExtract(url);  //（如果成功了的话）得到了homepages
        try {
            List<String> tempPages = getHomepages(url);  //以防初次抽取的homepages没抽成功，若没成功的话通过getHomepages继续抽
            if (tempPages != null && !tempPages.isEmpty()) {
                for (String link : tempPages) {
                    if(!homepage.contains(link)){//避免加入重复的链接
                        homepage += link + ";";
                    }
                }
                homepage = homepage.substring(0, homepage.lastIndexOf(";")); //将结果最后的“；”去除
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return homepage;
    }

    /**
     * 获取第一个link作为Homepage
     * @param url
     * @return
     */
    public  String extractLink(String url){
        String  homepage = url;
        doExtract(url);
        try {
            List<String> tempPages = getHomepages(url);
            homepage = (tempPages != null && !tempPages.isEmpty()) ? tempPages.get(0) : url;
        }catch (Exception e){
            e.printStackTrace();
        }
        return homepage;
    }

    /**
     * 抽取Homepage核心方法
     * @param url 抽取目标url链接
     */
    public static void doExtract(String url) {
        Spider.create(extractMutilLink4Openhub).addUrl(new String("https://www.openhub.net"+url)).thread(1).run();
    }

    public static void main(String[] args){
        String result = new ExtractMutilLink4Openhub().extractLink("/p/jQuery/links");
        System.out.println(result);
    }
}
